package com.utc.cards.table.jade.agent.rulesAgent;

import jade.core.Agent;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;

import com.utc.cards.model.HostModel;
import com.utc.cards.model.card.Card;
import com.utc.cards.model.deck.Deck;
import com.utc.cards.model.player.IPlayer;

public class RulesAgent extends Agent implements IRulesAgent
{
    private static final long serialVersionUID = -2248519337273621061L;
    private static Logger log = LoggerFactory.getLogger(RulesAgent.class);

    private HostModel model;
    private Context context;

    public HostModel getModel()
    {
        return model;
    }

    @Override
    protected void setup()
    {
        super.setup();
        Object[] args = getArguments();
        if (args != null && args.length > 0)
        {
            if (args[0] instanceof Context)
            {
                context = (Context) args[0];
            }
            else
            {
                log.error("Missing Context arg during agent setup");
            }
            if (args[1] instanceof HostModel)
            {
                model = (HostModel) args[1];
            }
            else
            {
                log.error("Missing HostModel arg during agent setup");
            }
        }

        // Add initial behaviours
        log.debug("add behaviour RulesListenerBehaviour to RulesAgent");
        addBehaviour(new RulesListenerBehaviour(this));

        // expose l'interface pour la rendre accessible par les activity
        registerO2AInterface(IRulesAgent.class, this);

        model.setRuleAvailability(true);
    }

    @Override
    public void validatePlayerCards(List<Card> cards, IPlayer player)
    {
        log.debug("add behaviour ValidateCardsBehaviour to RulesAgent");
        addBehaviour(new ValidateCardsBehaviour(this, cards, player));
    }

    @Override
    public void deteminateFirstPlayer()
    {
        log.debug("add behaviour FirstPlayerBehaviour to RulesAgent");
        addBehaviour(new FirstPlayerBehaviour(this, model));
    }

    @Override
    public void distributeInitialCards()
    {
        log.debug("add behaviour InitialCardDistributionBehaviour to RulesAgent");
        addBehaviour(new InitialCardDistributionBehaviour(RulesAgent.this));
    }

    @Override
    public void askAdvice(Deck hand, IPlayer player)
    {
        log.debug("add behaviour SendAdviceBehaviour to RulesAgent");
        addBehaviour(new SendAdviceBehaviour(this, hand, player));
    }

    @Override
    public void calculAndSendScore()
    {
        log.debug("add behaviour CalculScoreBehaviour to RulesAgent");
        addBehaviour(new CalculScoreBehaviour(this));
    }

    @Override
    public void determinateWinnerCurrentFold()
    {
        log.debug("add behaviour DeterminateWinnerCurrentFoldBehaviour to RulesAgent");
        addBehaviour(new DeterminateWinnerCurrentFoldBehaviour(this));
    }
}
